package Components.image.frame
{
	import Components.config.ImageConfig;
	import Components.image.ActionFileName;
	
	import core.RefreshManager;
	
	import events.RefreshEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.filesystem.File;
	
	public class BitmapMovieClip extends Sprite
	{
		private var _owner:DisplayObjectContainer;
		
		private var _action:String;
		private var _direction:String;
		
		private var _isPlaying:Boolean = false;
		private var _currentFrame:int = 0;
		private var _vecFrame:Vector.<BitmapFrame>;
		
		/**
		 * 记录时间长度  
		 */		
		private var _time:int = 0;
		
		public function BitmapMovieClip( owner:DisplayObjectContainer )
		{
			super();
			_owner = owner;
		}
		
		public function destroy():void
		{
			if( parent ) parent.removeChild( this );
			stop();
			while( _vecFrame.length > 0 ) _vecFrame.pop().destroy();
		}
		
		public function output():void
		{
			if( _action != ImageConfig.lastOutAction )
			{
				ImageConfig.lastOutAction = _action;
				var s:String = "var " + _action + "_l:int=" + _vecFrame.length + ";";
				ImageConfig.outAS += s;
			}
			for( var i:int=0; i<_vecFrame.length; i++ )
			{
				_vecFrame[i].out( _action, _direction, i );
			}
		}
		
		/**
		 * 装载图片 
		 * @param path 图片目录
		 * 
		 */		
		public function load( path:String, action:String, direction:String ):void
		{
			_action = ActionFileName.getOutActionName( action );
			_direction = ActionFileName.getOutDirectionName( direction );
			
			var file:File = new File( path );
			var arr:Array = file.getDirectoryListing();
			var arrFile:Array = new Array();
			for( var i:int=0; i<arr.length; i++ )
			{
				var f:File = arr[i];
				var ext:String = f.extension.toLowerCase();
				if( ext=="png" || ext=="jpg" )
				{
					arrFile.push( f );
				}
			}
			arrFile.sortOn( "name" );
			
			_vecFrame = new Vector.<BitmapFrame>();
			var str:String = _action + "_" + _direction + "_";
			for( i=0; i<arrFile.length; i++ )
			{
				var bf:BitmapFrame = new BitmapFrame( (arrFile[i] as File).nativePath );
				ImageConfig.arrOutBMClass.push( str+i );
				_vecFrame.push( bf );
			}
			_currentFrame = 0;
			if( _currentFrame < _vecFrame.length )
			{
				refresh();
			}
			play();
		}
		
		public function show():void
		{
			if( parent==null )
			{
				_owner.addChild( this );
			}
			_currentFrame = 0;
			play();
		}
		
		public function close():void
		{
			if( parent ) _owner.removeChild( this );
			stop();
		}
		
		public function play():void
		{
			if( !_isPlaying )
			{
				_isPlaying = true;
				_time = 0;
				RefreshManager.instance.addEventListener( RefreshEvent.REFRESH, onRefresh );
			}
		}
		
		public function stop():void
		{
			if( _isPlaying )
			{
				_isPlaying = false;
				RefreshManager.instance.removeEventListener( RefreshEvent.REFRESH, onRefresh );
			}
		}
		
		public function nextFrame():void
		{
			_currentFrame++;
			if( _currentFrame >= _vecFrame.length ) _currentFrame = 0;
			refresh();
		}
		
		private function onRefresh( e:RefreshEvent ):void
		{
			_time += e.data;
			if( _time >= ImageConfig.duration )
			{
				_time -= ImageConfig.duration;
				nextFrame();
			}
		}
		
		private function refresh():void
		{
			while( numChildren > 0 ) removeChildAt(0);
			addChild( _vecFrame[_currentFrame] );
		}
	}
}














